package cn.wangmeng.test;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Arrays;
import java.util.LinkedList;

public class DragListItem extends ListActivity {
	private static String[] items = { "lorem", "ipsum", "dolor", "sit" };
	private IconicAdapter adapter = null;
	private LinkedList<String> array = new LinkedList<String>(Arrays.asList(items));

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);

		adapter = new IconicAdapter();
		setListAdapter(adapter);

		TouchListView tlv = (TouchListView) getListView();

		tlv.setDropListener(onDrop);
//		tlv.setRemoveListener(onRemove);
	}

	private TouchListView.DropListener onDrop = new TouchListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			String item = adapter.getItem(from);

			adapter.remove(item);
			adapter.insert(item, to);
			
		}
	};

//	private TouchListView.RemoveListener onRemove = new TouchListView.RemoveListener() {
//		@Override
//		public void remove(int which) {
//			adapter.remove(adapter.getItem(which));
//		}
//	};

	class IconicAdapter extends ArrayAdapter<String> {
		IconicAdapter() {
			super(DragListItem.this, R.layout.row, array);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;

			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.row, parent, false);
			}

			row.setTag(position);
			TextView label = (TextView) row.findViewById(R.id.label);

			label.setText(array.get(position));

			return (row);
		}
	}
}